#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'实例属性和类属性'

class Student(object):
    def __init__(self,name):
        self.__name = name

#
stu = Student('MiMi')
# print(stu.__name) # AttributeError:

# 实例可以任意绑定其他属性
stu.x = 10
print(stu.x)

# 给类绑定属性
class Fruit(object):
    name = 'Fruit'

# 打印类的name属性
print(Fruit.name) # Fruit

# 因为实例并没有name属性，所以会继续查找class的name属性
f = Fruit()
print(f.name) # Fruit

# 给实例绑定name属性，由于实例的属性优先级比类属性高，所有它会屏蔽类属性
f.name = 'Apple'
print(f.name) # Apple

#案例
class Student1(object):
    count = 0

    def __init__(self, name):
        self.name = name
        Student1.count += 1

# 测试:
if Student1.count != 0:
    print('测试失败!')
else:
    bart = Student1('Bart')
    if Student1.count != 1:
        print('测试失败!')
    else:
        lisa = Student1('Bart')
        if Student1.count != 2:
            print('测试失败!')
        else:
            print('Students:', Student1.count)
            print('测试通过!')